package com.zmn.oms.services.impl.orderfinalpricequotation;

import com.zmn.base.plat.engine.common.constant.PlatEngineConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.orderfinalpricequotation.OrderFinalPriceQuotation;
import com.zmn.oms.model.entity.orderfinalpricequotation.OrderFinalPriceQuotationQuery;
import com.zmn.oms.persistence.interfaces.orderfinalpricequotation.OrderFinalPriceQuotationDao;
import com.zmn.oms.services.interfaces.orderfinalpricequotation.OrderFinalPriceQuotationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 一口价订单报价表接口实现类
 *
 * @author chengguang
 * @since  2022/09/19 16:14
 */
@Slf4j
@Service
public class OrderFinalPriceQuotationServiceImpl implements OrderFinalPriceQuotationService {

    private static final String TAG = "一口价订单报价表基础服务接口";

    @Resource
    private OrderFinalPriceQuotationDao orderFinalPriceQuotationDao;

    @Override
    public OrderFinalPriceQuotation findById(Long finalPriceId) {
        if (Objects.isNull(finalPriceId)) {
            log.error("[{}],根据id查询数据finalPriceId为null", TAG);
            return null;
        }
        return orderFinalPriceQuotationDao.findById(finalPriceId);
    }

    @Override
    public Integer findMaxSort() {
        return orderFinalPriceQuotationDao.findMaxSort();
    }

    @Override
    public List<OrderFinalPriceQuotation> listByIds(List<Long> finalPriceIds) {
        if (CollectionUtils.isEmpty(finalPriceIds)) {
            log.error("[{}],按条件查询数据,finalPriceIds为null", TAG);
            return Collections.emptyList();
        }
        return orderFinalPriceQuotationDao.listByIds(finalPriceIds);
    }

    @Override
    public List<OrderFinalPriceQuotation> listByQuery(OrderFinalPriceQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return orderFinalPriceQuotationDao.listByQuery(query);
    }

    @Override
    public List<OrderFinalPriceQuotation> listPageByQuery(OrderFinalPriceQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        Integer count = countByQuery(query);
        query.setTotalCount(count);
        if (Objects.equals(count,PlatEngineConsts.NONE)){
            return new ArrayList<>();
        }
        return orderFinalPriceQuotationDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(OrderFinalPriceQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return orderFinalPriceQuotationDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(OrderFinalPriceQuotation orderFinalPriceQuotation) {
        if (Objects.isNull(orderFinalPriceQuotation.getFinalPriceId())){
            log.error("[{}],根据id修改数据,orderFinalPriceQuotation为null", TAG);
            return null;
        }
        return orderFinalPriceQuotationDao.updateByKey(orderFinalPriceQuotation);
    }

    @Override
    public void updateStatusBatch(List<Long> ids, Integer status, String operator) {
        if (!Objects.equals(status, PlatEngineConsts.NO) && !Objects.equals(status, PlatEngineConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        orderFinalPriceQuotationDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(OrderFinalPriceQuotation orderFinalPriceQuotation){
        if (Objects.isNull(orderFinalPriceQuotation)){
            log.error("[{}],新增数据,orderFinalPriceQuotation为null", TAG);
            return null;
        }
        return orderFinalPriceQuotationDao.insert(orderFinalPriceQuotation);
    }

    @Override
    public void insertBatch(List<OrderFinalPriceQuotation> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        orderFinalPriceQuotationDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Long finalPriceId) {
        if (Objects.isNull(finalPriceId)){
            log.error("[{}],根据id删除数据,finalPriceId为null", TAG);
            return null;
        }
        return orderFinalPriceQuotationDao.deleteByKey(finalPriceId);
    }

}